#include "imagemodel.h"
ImageModel * ImageModel::instance = nullptr;

ImageModel *ImageModel::getInstance()
{
    if(ImageModel::instance == nullptr){
        ImageModel::instance = new ImageModel;
    }
    return ImageModel::instance;
}

int ImageModel::InsertImage(QString imagePath, QString datetime, double size, int userId)
{
    QString sql = QString("insert into tbl_image(image_path, image_date,image_size, user_id) values('%1', '%2', %3, %4); ").arg(imagePath).arg(datetime).arg(size).arg(userId);

//    qDebug() << sql;
    int res = SingletonBD::getInstance()->doUpdateInsertDel(sql);
    return res;
}

int ImageModel::selectImage(char **&qres, int &row, int &col)
{
    UserModel::getInstance()->selectUserId(qres, row, col);
    QString id = qres[1];
    qDebug() << "userId" << id;

    qres = nullptr;
    col=0,row=0;

    QString sql = QString("SELECT image_path, image_date FROM tbl_image a INNER JOIN tbl_user b ON a.user_id = b.user_id WHERE a.user_id = %1 ORDER BY image_date DESC;").arg(id);
    int res = SingletonBD::getInstance()->doSelect(sql, qres, row, col);
    if(res==0){
        if(row>0){  //有数据，更新设备信息
            qDebug() << "有结果";
            return 0;
        }
    }
    return -1;
}

ImageModel::ImageModel()
{

}
